<?php

namespace app\admin\controller;

use app\admin\service\recycle\Lists as RuinuoLists;
use app\admin\service\recycle\Logic as RuinuoLogic;
use think\facade\Db;
use think\facade\Request;
use app\common\middleware\Auth;
use app\common\middleware\Power;
use app\common\middleware\Log;

class Recycle
{
    protected $middleware = [Auth::class => [], Power::class => [], Log::class => []];

    /*列表模板及数据*/
    public function Index()
    {
        if (Request::isAjax() || Request::isPost()) {
            $RuinuoLists = new RuinuoLists();
            $data['rows'] = $RuinuoLists->Lists(input());//列表数据
            $data['total'] = $RuinuoLists->Total(input());//总数据
            return BackCode($data);
        }
        return ViewTpl('admin/recycle/lists');
    }

    /*Tab及统计*/
    public function TabSum()
    {
        if (Request::isAjax() || Request::isPost()) {
            $RuinuoLists = new RuinuoLists();
            $data['tab'] = $RuinuoLists->Tab(input());//Tab统计
            $data['sum'] = $RuinuoLists->Sum(input());//Sum统计
            return BackCode($data);
        }
    }

    /*明细*/
    public function Detail()
    {
        if (Request::isAjax() || Request::isPost()) {
            $RuinuoLogic = new RuinuoLogic();
            $data = $RuinuoLogic->Detail(input());
            return BackCode($data['data'], $data['code'], $data['msg']);
        }
        return ViewTpl('admin/recycle/form');
    }

    /*新增数据*/
    public function Add()
    {
        if (Request::isAjax() || Request::isPost()) {
            $RuinuoLogic = new RuinuoLogic();
            $data = $RuinuoLogic->Add(input());
            return BackCode($data['data'], $data['code'], $data['msg']);
        }
        return ViewTpl('admin/recycle/form');
    }

    /*修改数据*/
    public function Edit()
    {
        if (Request::isAjax() || Request::isPost()) {
            $RuinuoLogic = new RuinuoLogic();
            $data = $RuinuoLogic->Edit(input());
            return BackCode($data['data'], $data['code'], $data['msg']);
        }
        return ViewTpl('admin/recycle/form');
    }

    /*删除单条数据*/
    public function Delete()
    {
        if (Request::isAjax() || Request::isPost()) {
            $RuinuoLogic = new RuinuoLogic();
            $data = $RuinuoLogic->Delete(input('id'), 1);
            return BackCode($data['data'], $data['code'], $data['msg']);
        }
    }

    /*批量删除数据*/
    public function batch_delete()
    {
        if (Request::isAjax() || Request::isPost()) {
            $RuinuoLogic = new RuinuoLogic();
            $data = $RuinuoLogic->Delete(input('ids'), 2);
            return BackCode($data['data'], $data['code'], $data['msg']);
        }
    }

    /*单个字段修改*/
    public function update_field()
    {
        if (Request::isAjax() || Request::isPost()) {
            $RuinuoLogic = new RuinuoLogic();
            $data = $RuinuoLogic->UpdateField(input());
            return BackCode($data['data'], $data['code'], $data['msg']);
        }
    }

    /*导入Excel数据*/
    public function import_excel()
    {
        if (Request::isAjax() || Request::isPost()) {
            $file = Request::file("file") ?: Request::file("edit");
            $RuinuoLists = new RuinuoLists();
            $data = $RuinuoLists->InExcel($file);
            return BackCode($data['data'], $data['code'], $data['msg']);
        }
    }

    /*导出Excel数据*/
    public function export_excel()
    {
        $RuinuoLists = new RuinuoLists();
        $RuinuoLists->OutExcel(input());
    }

    /*下载Excel模板*/
    public function template_excel()
    {
        $RuinuoLists = new RuinuoLists();
        $RuinuoLists->TemplateExcel();
    }

    /*恢复数据*/
    public function Restore()
    {
        if (Request::isAjax() || Request::isPost()) {
            if (empty(input("id"))) {
                return BackCode([], 203, '参数不能为空');
            }
            $find = Db::name("admin_recycle")->where("id='" . input("id") . "' and is_status=2")->field("delete_id,table_name,delete_type,delete_data")->find();
            if (empty($find)) {
                return BackCode([], 203, '没有此条数据或者已恢复');
            }
            switch ($find['delete_type']) {
                case 1:
                    Db::name($find['table_name'])->insert($find);
                    break;
                case 2:
                    Db::name($find['table_name'])->where("id='" . $find['delete_id'] . "'")->update(['is_delete' => 0]);
                    break;
            }
            Db::name("admin_recycle")->where("id='" . input("id") . "'")->update(['is_status' => 1]);
            return BackCode();
        }
    }


}